/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.kategori;

import com.x3.purchasing.common.ClassApplicationModule;
import java.util.List;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class KategoriWnd extends ClassApplicationModule {

    private KategoriDAO kategoriDAO;
    private Textbox textboxNama;
    private Listbox listboxData;
    private Kategori kategoriEdit = null;

    public void onCreate() throws Exception {
        kategoriDAO = new KategoriDAOImpl();
        textboxNama = (Textbox) getFellow("textboxNama");
        listboxData = (Listbox) getFellow("listboxData");
        load();
    }

    private void load() throws InterruptedException {
        try {
            List<Kategori> data = kategoriDAO.gets();
            listboxData.getItems().clear();
            int index = 1;
            for (final Kategori kategori : data) {
                Listitem item = new Listitem();
                item.setValue(kategori);
                item.appendChild(new Listcell(index + ""));
                item.appendChild(new Listcell(kategori.getNama()));

                Toolbarbutton btnEdit = new Toolbarbutton();
                btnEdit.setId("btnEdit" + index);
                btnEdit.setTooltiptext("Klik untuk mengubah data");
                btnEdit.setImage("/img/edit.png");
                btnEdit.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        textboxNama.setValue(kategori.getNama());
                        kategoriEdit = kategori;
                        textboxNama.setFocus(true);
                    }
                });

                Toolbarbutton btnDelete = new Toolbarbutton();
                btnDelete.setId("btnDelete" + index);
                btnDelete.setTooltiptext("Klik untuk menghapus data");
                btnDelete.setImage("/img/delete.png");
                btnDelete.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        delete(kategori.getId());
                    }
                });

                Listcell cellAksi = new Listcell();
                cellAksi.appendChild(btnEdit);
                cellAksi.appendChild(btnDelete);

                item.appendChild(cellAksi);
                listboxData.appendChild(item);
                index++;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Messagebox.show("Gagal menampilkan data Kualifikasi");
        }
    }

    public void cancel() throws Exception {
        textboxNama.setValue("");
        kategoriEdit = null;
    }

    public void save() throws Exception {
        try {
            if (!textboxNama.getValue().isEmpty()) {
                if (kategoriEdit == null) {//insert
                    Kategori kategori = new Kategori();
                    kategori.setNama(textboxNama.getValue());
                    kategoriDAO.insert(kategori);
                } else {//update
                    kategoriEdit.setNama(textboxNama.getValue());
                    kategoriDAO.update(kategoriEdit);
                    kategoriEdit = null;
                }
                load();
                Messagebox.show("Data tersimpan");
                textboxNama.setValue("");
            } else {
                Messagebox.show("Silahkan input nama kategori");
            }
        } catch (Exception ex) {
            //ex.printStackTrace();
            Messagebox.show("Gagal menyimpan data");
        }
    }

    private void delete(int id) throws InterruptedException, Exception {
        if (Messagebox.show("Hapus?", "Konfirmasi", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION) == 1) {
            kategoriDAO.delete(id);
            load();
        }
    }
}
